package com.ourteam.product.api;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ourteam.frame.controller.AbstractCRUDServiceController;
import com.ourteam.product.ui.dao.ProductUIListDAOQueryBean;
import com.ourteam.product.ui.domain.ProductUIListBean;
import com.ourteam.product.ui.domain.ProductUIListOptionBean;
import com.ourteam.product.ui.service.IProductUIService;
import com.ourteam.product.ui.service.ProductUIServiceFactory;

import net.ui.model.constlist.ListData;
import net.ui.model.constlist.ListDataContainer;

@Controller
@RequestMapping("/api/PRODUCT/UI/productUIList")
public class ProductUIListAPI
		extends AbstractCRUDServiceController<ProductUIListDAOQueryBean, ProductUIListSearchModel, ProductUIListBean> {

	private static final IProductUIService PRODUCT_UI_SERVICE = ProductUIServiceFactory.getProductUIService();

	@Override
	protected ProductUIListDAOQueryBean newQueryBean() {
		return new ProductUIListDAOQueryBean();
	}

	@Override
	protected int queryCount(ProductUIListDAOQueryBean queryBean) throws Exception {
		return PRODUCT_UI_SERVICE.queryProductUIListCount(queryBean);
	}

	@Override
	protected ProductUIListBean[] queryData(ProductUIListDAOQueryBean queryBean) throws Exception {
		return PRODUCT_UI_SERVICE.queryProductUILists(queryBean);
	}

	@Override
	protected void deleteData(long[] ids) throws Exception {
		PRODUCT_UI_SERVICE.deleteProductUILists(ids);
	}

	@Override
	protected ProductUIListBean getDataById(long id) throws Exception {
		return PRODUCT_UI_SERVICE.getProductUIListById(id);
	}

	@Override
	protected void saveData(ProductUIListBean dataBean) throws Exception {
		dataBean.setLastModifyTime(new Date());
		dataBean.setLastModifyUser(getLoginUserName());
		if (dataBean.getId() == 0) {
			PRODUCT_UI_SERVICE.addNewProductUIList(dataBean);
		} else {
			PRODUCT_UI_SERVICE.modifyProductUIList(dataBean);
		}
	}

	@RequestMapping("/getList")
	@ResponseBody
	public Map<String, Object> getList(@RequestParam long id) throws Exception {

		ProductUIListBean productUIListBean = PRODUCT_UI_SERVICE.getProductUIListById(id);

		ListDataContainer listDataContainer = new ListDataContainer();

		listDataContainer.setDataProperty(productUIListBean.getDataProperty());

		listDataContainer.setDesc(productUIListBean.getRemarks());

		listDataContainer.setLabelProperty(productUIListBean.getLabelProperty());

		listDataContainer.setName(String.valueOf(productUIListBean.getId()));

		listDataContainer.setType(productUIListBean.getType());
		
		Map<String, Object> map = new HashMap<>();
		map.put("name", productUIListBean.getCode());
		map.put("dataProperty", productUIListBean.getDataProperty());
		map.put("labelProperty", productUIListBean.getLabelProperty());
		//map.put("iconProperty", productUIListBean.);
		map.put("type", productUIListBean.getType());
		//map.put("script", dataContainer.getScript());
		//map.put("datasource", dataContainer.getDatasource());
		//map.put("datatype", dataContainer.getDatatype());
		map.put("desc", productUIListBean.getRemarks());
		//map.put("searchProperty", dataContainer.getSearchProperty());

		ProductUIListOptionBean[] listOptionBeans = PRODUCT_UI_SERVICE
				.getProductUIListOptions(productUIListBean.getId());

		for (int j = 0; j < listOptionBeans.length; j++) {

			ProductUIListOptionBean productUIListOptionBean = listOptionBeans[j];

			ListData listData = new ListData();

			listData.setListData(productUIListOptionBean.getDataValue(), productUIListOptionBean.getDataLabel(),
					productUIListOptionBean.getDataIcon());

			listDataContainer.addListData(listData);
		}
		
		map.put("options", listDataContainer);

		return map;

	}

}
